<?php
namespace app\admin\controller;
use think\Db;
class Bill extends Base
{
   //列表
    public function lists()
    {
        if(is_post()){
            $is_down = input('is_down');
            $cur_page     = input('page');
            $type     = input('type');
            $money_type  = input('money_type');
            $keyword  = input('keyword');
            $min_time = input('min_time');
            $max_time = input('max_time');
            //原生态sql查询
            $sql = "
                select
                    [1]
                from
                    bill a
                left join
                    user b
                on
                    a.user_id=b.id
                ";
            $param = [];
            $w = ['where'];
            if ( $type != '100all' ) {
                $w[] = " and a.type = ?";
                $param[] = $type;
            }

            if ( $money_type != '100all' ) {
                $w[] = " and a.money_type = ?";
                $param[] = $money_type;
            }
            if ( !empty($keyword) ) {
                $w[] = " and (a.order_no like ? or b.name like ? or b.phone like ?)";
                $param[] = "%{$keyword}%";
                $param[] = "%{$keyword}%";
                $param[] = "%{$keyword}%";
            }
            if ( !empty($min_time) ) {
                $w[]    = " and a.create_time >= ?";
                $param[] = strtotime($min_time);
            }
            if ( !empty($max_time) ) {
                $w[]    = " and a.create_time <= ?";
                $param[] = strtotime($max_time);
            }
            
            #条件判断
            if (count($w)>1){
              $w[1] = str_replace("and","",$w[1]);
              $sql .= implode(" ",$w);
            }

             //汇总
            $d = [
                ["总数","count(*) as nums", ""],
                ["加钱","sum(a.money) as nums", " and a.show_money>0"],
                ["减钱","sum(a.money) as nums", " and a.show_money<0"],
            ];
            $count_html = com_list_count($sql, $param,$d);
            
            #分页
            $page_size = c('a.page_size');
            $page_html = com_list_page($sql, $param, $cur_page,$page_size);

            #内容
            $start = ($cur_page - 1) * $page_size;
            $field = "a.*, b.name,b.user_no";
            $sql_list = str_replace("[1]",$field,$sql);
            $sql_list .= $is_down ? " order by a.id desc" : " order by a.id desc limit {$start},{$page_size}";

            $rslist = Db::query($sql_list, $param);
            foreach ($rslist as $k => $v) {
                $rslist[$k]['type']         = c("type.bill_type",$v['type']);
                $rslist[$k]['money_type']         = c("type.money_type",$v['money_type']);
                $rslist[$k]['before_money'] = com_number($v['before_money']);
                $rslist[$k]['after_money']  = com_number($v['after_money']);
                $rslist[$k]['fee']  = $v['fee'];
                $rslist[$k]['create_time']  = com_formate_time($v['create_time']);
            }

            if($is_down){
                $url = com_down_csv($rslist,
                ['ID','编号','姓名','类型','币种','之前','金额','之后','备注','时间'],
                ["id","user_no","name","type","money_type", "before_money","show_money","after_money", "note","create_time"],'bill','xls');
                com_check(['data'=>$url,'suc_js'=>'function.openurl']);
            }
            $body_html = com_list_body($rslist, ["id","user_no","name","type","money_type", "before_money","show_money","after_money", "note","create_time"]);
            com_check(['data'=>['com_body'=>$body_html, 'com_page'=>$page_html,'com_count'=>$count_html]]);
        }
        $data = [
            'type' => ['100all'=>'全部类型']+c('type.bill_type'),
            'money_type' => ['100all'=>'全部币种']+c('type.money_type'),
        ];
        return view('',$data);
    }

    //会员账单列表
    public function blists($id='')
    {
        if(is_post()){
            $cur_page     = input('page');
            $type     = input('type');
            $id     = input('id');
            $money_type  = input('money_type');
            $keyword  = input('keyword');
            $min_time = input('min_time');
            $max_time = input('max_time');
            //原生态sql查询
            $sql = "
                select
                    [1]
                from
                    bill a
                left join
                    user b
                on
                    a.user_id=b.id
                ";
            $param = [];
            $w = ['where'];
            $w[] .= " and a.user_id = ?";
            $param[] = $id;
            if ( $type != '100all' ) {
                $w[] .= " and a.type = ?";
                $param[] = $type;
            }

            if ( $money_type != '100all' ) {
                $w[] .= " and a.money_type = ?";
                $param[] = $money_type;
            }
            if ( !empty($keyword) ) {
                $w[] .= " and (a.order_no like ? or b.name like ? or b.phone like ?)";
                $param[] = "%{$keyword}%";
                $param[] = "%{$keyword}%";
                $param[] = "%{$keyword}%";
            }
            if ( !empty($min_time) ) {
                $w[]    .= " and a.create_time >= ?";
                $param[] = strtotime($min_time);
            }
            if ( !empty($max_time) ) {
                $w[]    .= " and a.create_time <= ?";
                $param[] = strtotime($max_time);
            }
            
            #条件判断
            if (count($w)>1){
              $w[1] = str_replace("and","",$w[1]);
              $sql .= implode(" ",$w);
            }
            #分页
            $page_size = c('a.page_size');
            $page_html = com_list_page($sql, $param, $cur_page,$page_size);

            #内容
            $start = ($cur_page - 1) * $page_size;
            $field = "a.*, b.name";
            $sql_list = str_replace("[1]",$field,$sql);
            $sql_list .= " order by a.id desc limit {$start},{$page_size}";

            $rslist = Db::query($sql_list, $param);
            foreach ($rslist as $k => $v) {
                $rslist[$k]['type']         = c("type.bill_type",$v['type']);
                $rslist[$k]['money_type']         = c("type.money_type",$v['money_type']);
                $rslist[$k]['before_money'] = com_number($v['before_money']);
                $rslist[$k]['after_money']  = com_number($v['after_money']);
                $rslist[$k]['fee']  = $v['fee'];
                $rslist[$k]['create_time']  = com_formate_time($v['create_time']);
            }
            $body_html = com_list_body($rslist, ["id","name","type","money_type", "before_money","show_money","after_money", "note","fee","create_time"]);
            com_check(['data'=>['com_body'=>$body_html, 'com_page'=>$page_html]]);
        }
        $data = [
            'id'  => $id,
            'type' => ['100all'=>'全部类型']+c('type.bill_type'),
            'money_type' => ['100all'=>'全部币种']+c('type.money_type'),
        ];
        return view('',$data);
    }

    //收益列表
    public function dlists($id='')
    {
        if(is_post()){
            $cur_page     = input('page');
            $type     = input('type');
            $id     = input('id');
            $money_type  = input('money_type');
            $keyword  = input('keyword');
            $min_time = input('min_time');
            $max_time = input('max_time');
            //原生态sql查询
            $sql = "
                select
                    [1]
                from
                    bill a
                left join
                    user b
                on
                    a.user_id=b.id
                ";
            $param = [];
            $w = ['where'];
            $w[] .= " and a.user_id = ?";
            $param[] = $id;
            $w[] .= " and a.type in(100,102,104,106)";
            if ( $type != '100all' ) {
                $w[] .= " and a.type = ?";
                $param[] = $type;
            }

            if ( $money_type != '100all' ) {
                $w[] .= " and a.money_type = ?";
                $param[] = $money_type;
            }
            if ( !empty($keyword) ) {
                $w[] .= " and (a.order_no like ? or b.name like ? or b.phone like ?)";
                $param[] = "%{$keyword}%";
                $param[] = "%{$keyword}%";
                $param[] = "%{$keyword}%";
            }
            if ( !empty($min_time) ) {
                $w[]    .= " and a.create_time >= ?";
                $param[] = strtotime($min_time);
            }
            if ( !empty($max_time) ) {
                $w[]    .= " and a.create_time <= ?";
                $param[] = strtotime($max_time);
            }
            
            #条件判断
            if (count($w)>1){
              $w[1] = str_replace("and","",$w[1]);
              $sql .= implode(" ",$w);
            }
            #分页
            $page_size = c('a.page_size');
            $page_html = com_list_page($sql, $param, $cur_page,$page_size);

            #内容
            $start = ($cur_page - 1) * $page_size;
            $field = "a.*, b.name";
            $sql_list = str_replace("[1]",$field,$sql);
            $sql_list .= " order by a.id desc limit {$start},{$page_size}";

            $rslist = Db::query($sql_list, $param);
            foreach ($rslist as $k => $v) {
                $rslist[$k]['type']         = c("type.bill_type",$v['type']);
                $rslist[$k]['money_type']         = c("type.money_type",$v['money_type']);
                $rslist[$k]['before_money'] = com_number($v['before_money']);
                $rslist[$k]['after_money']  = com_number($v['after_money']);
                $rslist[$k]['fee']  = $v['fee'];
                $rslist[$k]['create_time']  = com_formate_time($v['create_time']);
            }
            $body_html = com_list_body($rslist, ["id","name","type","money_type", "before_money","show_money","after_money", "note","fee","create_time"]);
            com_check(['data'=>['com_body'=>$body_html, 'com_page'=>$page_html]]);
        }
        $data = [
            'id'  => $id,
            'type' => ['100all'=>'全部类型']+c('type.bill_type'),
            'money_type' => ['100all'=>'全部币种']+c('type.money_type'),
        ];
        return view('',$data);
    }
}
